package UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import DBTasks.CurrentQuarterlyReport;
import DBTasks.Summary;

@SuppressWarnings("serial")
public class QuarterlyPanel extends JPanel implements ActionListener{

	private JButton returnButton;
	private DefaultTableModel model1, model2, model3;
	private JTable table1, table2, table3;
	private JTableHeader headers1, headers2, headers3;
	private MainFrame frame;

	@SuppressWarnings("deprecation")
	public QuarterlyPanel(MainFrame mainFrame) {
		
		this.setSize(800, 600);
		this.frame = mainFrame;
		
		JLabel logo = new JLabel("Atlanta Pet Adoption Consortium");
		logo.setFont(new Font("Tahoma", Font.BOLD, 30));
		
		JLabel shelterLabel = new JLabel("Current Quarterly Report");
		shelterLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		
		JLabel dateLabel = new JLabel("Date: " + dateFormat.format(date));
		dateLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		returnButton = new JButton("Return");
		returnButton.addActionListener(this);
		
		String[] columnNames = {"Shelter", "No. of Sponsored Events", "Total Donations", "Total Pet Adoptions", "Total Pets Received"};
		
		ArrayList<Summary> summaries = CurrentQuarterlyReport.generateCurrentQuarterlyReport();
		String[] currentMonths = CurrentQuarterlyReport.calculateMonths();
				
		ArrayList<Summary> summaries1 = new ArrayList<Summary>();
		ArrayList<Summary> summaries2 = new ArrayList<Summary>();
		ArrayList<Summary> summaries3 = new ArrayList<Summary>();
		
		for(Summary s : summaries) {
			
			if(s.getMonth() == currentMonths[0]) {
				summaries1.add(s);
			}
			
			else if(s.getMonth() == currentMonths[1]) {
				summaries2.add(s);
			}

			else if(s.getMonth() == currentMonths[2]) {
				summaries3.add(s);
            }
			
			else {
				summaries1.add(s);
			}
			
		}
		
		Object[][] data1 = new Object[summaries1.size()][5];
		Object[][] data2 = new Object[summaries2.size()][5];
		Object[][] data3 = new Object[summaries3.size()][5];
		
		for(int i = 0; i < data1.length; i++) {
			data1[i][0] = summaries1.get(i).getShelterName();
			data1[i][1] = summaries1.get(i).getEventCount();
			data1[i][2] = "$" + summaries1.get(i).getTotalDonations();
			data1[i][3] = summaries1.get(i).getTotalAdoptions();
			data1[i][4] = summaries1.get(i).getPetsReceived();
		}
		
		for(int i = 0; i < data2.length; i++) {
			data2[i][0] = summaries2.get(i).getShelterName();
			data2[i][1] = summaries2.get(i).getEventCount();
			data2[i][2] = "$" + summaries2.get(i).getTotalDonations();
			data2[i][3] = summaries2.get(i).getTotalAdoptions();
			data2[i][4] = summaries2.get(i).getPetsReceived();
		}
		
		for(int i = 0; i < data3.length; i++) {
			data3[i][0] = summaries3.get(i).getShelterName();
			data3[i][1] = summaries3.get(i).getEventCount();
			data3[i][2] = "$" + summaries3.get(i).getTotalDonations();
			data3[i][3] = summaries3.get(i).getTotalAdoptions();
			data3[i][4] = summaries3.get(i).getPetsReceived();
		}
		
		model1 = new DefaultTableModel(data1, columnNames);
		table1 = new JTable(model1);
		table1.setGridColor(Color.black);
		headers1 = table1.getTableHeader();
		table1.setBorder(BorderFactory.createLineBorder(Color.black));
		headers1.setBorder(BorderFactory.createLineBorder(Color.black));
		table1.setRowHeight(30);
		
		model2 = new DefaultTableModel(data2, columnNames);
		table2 = new JTable(model2);
		table2.setGridColor(Color.black);
		headers2 = table2.getTableHeader();
		table2.setBorder(BorderFactory.createLineBorder(Color.black));
		headers2.setBorder(BorderFactory.createLineBorder(Color.black));
		table2.setRowHeight(30);
		
		model3 = new DefaultTableModel(data3, columnNames);
		table3 = new JTable(model3);
		table3.setGridColor(Color.black);
		headers3 = table3.getTableHeader();
		table3.setBorder(BorderFactory.createLineBorder(Color.black));
		headers3.setBorder(BorderFactory.createLineBorder(Color.black));
		table3.setRowHeight(30);
		
		JLabel monthLabel1 = new JLabel(currentMonths[0] + " " + (date.getYear() + 1900));
		monthLabel1.setFont(new Font("Tahoma", Font.PLAIN, 15));
		JLabel monthLabel2 = new JLabel(currentMonths[1] + " " + (date.getYear() + 1900));
		monthLabel2.setFont(new Font("Tahoma", Font.PLAIN, 15));
		JLabel monthLabel3 = new JLabel(currentMonths[2] + " " + (date.getYear() + 1900));
		monthLabel3.setFont(new Font("Tahoma", Font.PLAIN, 15));
		
        GroupLayout gl = new GroupLayout(this);
		
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(logo)
				.addGroup(gl.createSequentialGroup()
						.addComponent(shelterLabel)
						.addGap(450)
						.addComponent(dateLabel))
				.addComponent(monthLabel1)
				.addComponent(headers1)
				.addComponent(table1)
				.addComponent(monthLabel2)
				.addComponent(headers2)
				.addComponent(table2)
				.addComponent(monthLabel3)
				.addComponent(headers3)
				.addComponent(table3)
				.addGroup(gl.createSequentialGroup()
						.addGap(650)
						.addComponent(returnButton)));
		
	    gl.setVerticalGroup(gl.createSequentialGroup()
	    		.addGap(15)
			    .addComponent(logo)
			    .addGap(40)
			    .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(shelterLabel)
						.addComponent(dateLabel))
			    .addGap(20)
			    .addComponent(monthLabel1)
			    .addGap(5)
			    .addComponent(headers1)
			    .addComponent(table1)
			    .addGap(40)
			    .addComponent(monthLabel2)
			    .addGap(5)
			    .addComponent(headers2)
			    .addComponent(table2)
			    .addGap(40)
			    .addComponent(monthLabel3)
			    .addGap(5)
			    .addComponent(headers3)
			    .addComponent(table3)
			    .addGap(25)
			    .addComponent(returnButton)
			    .addGap(40)
	    );
		
		gl.setAutoCreateContainerGaps(true);
		
		this.setLayout(gl);
		
	}
	
	public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillRect(0, 0, this.getWidth(), 70);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(returnButton)) {
			frame.setPanel(frame.getHomepage());
		}
	}

}
